package com.example.controller;

import com.example.pojo.Salary;
import com.example.service.LeaveService;
import com.example.service.SalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class SalaryController {

    @Autowired
    private SalaryService salaryService;

    @Autowired
    private LeaveService leaveService;

    @PutMapping("/salary/update")
    public Map<String, Object> updateSalary(@RequestBody Salary salary) {
        Map<String, Object> response = new HashMap<>();
        try {
            // 获取当前工资信息
            Salary existingSalary = salaryService.getSalaryByEmployeeId(salary.getEmployeeId());
            if (existingSalary != null) {
                // 更新工资信息
                existingSalary.setBasicSalary(salary.getBasicSalary());
                existingSalary.setBonus(salary.getBonus());
                existingSalary.setPersonalTax(salary.getPersonalTax());
                existingSalary.setNationalTax(salary.getNationalTax());
                existingSalary.setHealthInsurance(salary.getHealthInsurance());
                existingSalary.setRetirementInsurance(salary.getRetirementInsurance());

                // 重新计算净工资
                int deductions = existingSalary.getPersonalTax() +
                        existingSalary.getNationalTax() +
                        existingSalary.getHealthInsurance() +
                        existingSalary.getRetirementInsurance();
                int netSalary = existingSalary.getBasicSalary() +
                        existingSalary.getBonus() -
                        deductions;
                existingSalary.setNetSalary(netSalary);

                // 更新工资信息
                salaryService.updateSalary(existingSalary);

                response.put("status", "success");
                response.put("message", "Salary information updated successfully.");
            } else {
                response.put("status", "error");
                response.put("message", "Salary information not found.");
            }
        } catch (Exception e) {
            response.put("status", "error");
            response.put("message", "Error updating salary information.");
        }
        return response;
    }

    @GetMapping("/salary/{employeeId}")
    public Map<String, Object> getSalaryByEmployeeId(@PathVariable String employeeId) {
        Map<String, Object> response = new HashMap<>();
        Salary salary = salaryService.getSalaryByEmployeeId(employeeId);

        if (salary != null) {
            LocalDate now = LocalDate.now();
            int leaveDays = leaveService.getApprovedLeaveDaysForMonth(employeeId, now.getYear(), now.getMonthValue());
            int leaveDeduction = (salary.getBasicSalary() / now.lengthOfMonth()) * leaveDays;

            salary.setNetSalary(salary.getNetSalary() - leaveDeduction);
            salary.setLeaveDays(leaveDays);
            salary.setLeaveDeduction(leaveDeduction);

            response.put("status", "success");
            response.put("salary", salary);
        } else {
            response.put("status", "error");
            response.put("message", "Salary information not found.");
        }

        return response;
    }
}
